from struct import Struct
from collections import namedtuple

class UnpackedData:
    def __init__(self, iName, iStructure):
        lFormat = "<" # LittleEndian
        lFields = ""
        for lVarField, lVarFormat in iStructure:
            lFormat += lVarFormat
            if lFields:
                lFields += " "
            lFields += lVarField
        self.mStruct = Struct(lFormat)
        self.mNamedTuple = namedtuple(iName, lFields)
    
    def unpackFromFile(self, iFile):
        return self.unpackWithOffset(iFile.read(self.mStruct.size), 0)[0]
        
    def unpackWithOffset(self, iData, iOffset):
        lnewOffset = iOffset + self.mStruct.size
        return self.mNamedTuple._make(self.mStruct.unpack(iData[iOffset:lnewOffset])), lnewOffset
